package net.dgg.zz.production.statistics.service;

import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.tmd.foundation.platform.common.util.TranslateUtil;
import net.dgg.tmd.foundation.platform.org.service.OrganizationManager;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.production.statistics.dao.StatisticsDao;
import net.dgg.zz.production.statistics.vo.OrderDetailVo;
import net.dgg.zz.production.statistics.vo.OrderRoyaltyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class StatisticsService {
    @Autowired
    private StatisticsDao statisticsDao;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private OrganizationManager organizationManager;
    @Resource
    private TranslateUtil translateUtil;
    @Resource
    private DesUtil desUtil;

    public List<Map> getOderorderDeductionList(Map params){
            List<Map> newList = new ArrayList<>();
          if(null ==params.get("signOrgId")){
              UserEntity userEntity = sessionManager.getCurrentUser();
              List<Long> listIds = organizationManager.findOrgIdByAncestorId(userEntity.getOrgId());
              params.put("orgList",listIds);
          }else{
              List<Long> listIds = organizationManager.findOrgIdByAncestorId(Long.valueOf(params.get("signOrgId").toString()));
              params.put("orgList",listIds);
          }
        Integer count =this.count(params);
         List<Map> list = statisticsDao.queryOderorderDeductionList(params);
          if(list.size()>0){
              for(Map map : list){
                  String personalName = queryPersonal(Long.valueOf(map.get("id").toString()));
                  map.put("personalName",personalName);
                  newList.add(map);
              }
          }
        params.put("count", count);
        return newList;
    }

    public Integer count(Map params){
        return statisticsDao.count(params);
    }

    /**
     * 查询使用的证书人名
     * @param id
     * @return
     */
    public String queryPersonal(Long id){
        return statisticsDao.queryPersonal(id);
    }

    /**
     * 提成节点列表
     * @return
     */
    public List<Map> extractNode(){
        return statisticsDao.queryExtractNode();
    }

    /**
     * 接单明细
     * @param params
     * @return
     */
    public List<Map> queryOrderDtail(Map params){
        if(null ==params.get("signOrgId")){
            UserEntity userEntity = sessionManager.getCurrentUser();
            List<Long> listIds = organizationManager.findOrgIdByAncestorId(userEntity.getOrgId());
            params.put("orgList",listIds);
        }else{
            List<Long> listIds = organizationManager.findOrgIdByAncestorId(Long.valueOf(params.get("signOrgId").toString()));
            params.put("orgList",listIds);
        }
        return statisticsDao.queryOderDetail(params);
    }
    public Integer orderCount(Map params){
        if(null ==params.get("signOrgId")){
            UserEntity userEntity = sessionManager.getCurrentUser();
            List<Long> listIds = organizationManager.findOrgIdByAncestorId(userEntity.getOrgId());
            params.put("orgList",listIds);
        }else{
            List<Long> listIds = organizationManager.findOrgIdByAncestorId(Long.valueOf(params.get("signOrgId").toString()));
            params.put("orgList",listIds);
        }
        return statisticsDao.oderCount(params);
    }

    /**
     * 获取产品列表
     * @return
     */
    public List<Map> queryProduct(){ return statisticsDao.queryProduct();}

    /**
     * 获取完成节点列表
     * @return
     */
    public List<Map> queryfinshNode(){ return statisticsDao.queryfinshNode();}

    /**
     * 订单提成导出
     * @param params
     * @return
     */
    public List<OrderRoyaltyVo> exportOrderRoyalty(Map params) {
        List<Map> listPage = this.getOderorderDeductionList(params);
        String[] transColum = {"flowType","status"};
        listPage = translateUtil.translateList(transColum, listPage);//翻译
        String[] hideColum = {"customerPhone"};//解密
        listPage = desUtil.decryptListMap(hideColum, listPage);
        CustomerNumberEncryptionUtil.encryptionList(hideColum, listPage);
        List<OrderRoyaltyVo> voList = new LinkedList<>();
        for(Map map : listPage){
            OrderRoyaltyVo orderRoyaltyVo = new OrderRoyaltyVo();
            orderRoyaltyVo.setScProductOrderNo(String.valueOf(map.get("scProductOrderNo") == null ? "" : map.get("scProductOrderNo")));
            orderRoyaltyVo.setBusinessTypeName(String.valueOf(map.get("businessTypeName") == null ? "" : map.get("businessTypeName")));
            orderRoyaltyVo.setCompanyName(String.valueOf(map.get("companyName") == null ? "" : map.get("companyName")));
            orderRoyaltyVo.setContractNo(String.valueOf(map.get("contractNo") == null ? "" : map.get("contractNo")));
            orderRoyaltyVo.setCustomerName(String.valueOf(map.get("customerName") == null ? "" : map.get("customerName")));
            orderRoyaltyVo.setFlowOrgName(String.valueOf(map.get("flowOrgName") == null ? "" : map.get("flowOrgName")));
            orderRoyaltyVo.setFlowType(String.valueOf(map.get("flowType") == null ? "" : map.get("flowType")));
            orderRoyaltyVo.setFlowUserName(String.valueOf(map.get("flowUserName") == null ? "" : map.get("flowUserName")));
            orderRoyaltyVo.setNodeName(String.valueOf(map.get("nodeName") == null ? "" : map.get("nodeName")));
            orderRoyaltyVo.setNodeOpetateTime(String.valueOf(map.get("nodeOpetateTime") == null ? "" : map.get("nodeOpetateTime")));
            orderRoyaltyVo.setPersonalName(String.valueOf(map.get("personalName") == null ? "" : map.get("personalName")));
            orderRoyaltyVo.setProductAttr(String.valueOf(map.get("productAttr") == null ? "" : map.get("productAttr")));
            orderRoyaltyVo.setProductName(String.valueOf(map.get("productName") == null ? "" : map.get("productName")));
            voList.add(orderRoyaltyVo);
        }
        return voList;
    }
    /**
     * 接单明细导出
     * @param params
     * @return
     */
    public List<OrderDetailVo> exportOrderdetail(Map params) {
        if(null ==params.get("signOrgId")){
            UserEntity userEntity = sessionManager.getCurrentUser();
            List<Long> listIds = organizationManager.findOrgIdByAncestorId(userEntity.getOrgId());
            params.put("orgList",listIds);
        }else{
            List<Long> listIds = organizationManager.findOrgIdByAncestorId(Long.valueOf(params.get("signOrgId").toString()));
            params.put("orgList",listIds);
        }
        List<Map> listPage = statisticsDao.exportList(params);
        String[] transColum = {"flowType","status"};
        listPage = translateUtil.translateList(transColum, listPage);//翻译
        List<OrderDetailVo> voList = new LinkedList<>();
        for(Map map : listPage){
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setScProductOrderNo(String.valueOf(map.get("scProductOrderNo") == null ? "" : map.get("scProductOrderNo")));
            orderDetailVo.setBusinessProductName(String.valueOf(map.get("businessProductName") == null ? "" : map.get("businessProductName")));
            orderDetailVo.setCertificationStr(String.valueOf(map.get("certificationStr") == null ? "" : map.get("certificationStr")));
            orderDetailVo.setCompanyName(String.valueOf(map.get("companyName") == null ? "" : map.get("companyName")));
            orderDetailVo.setContractCycle(String.valueOf(map.get("contractStartDate") == null ? "" : map.get("contractStartDate"))+"-"+String.valueOf(map.get("contractEndDate") == null ? "" : map.get("contractEndDate")));
            orderDetailVo.setContractEndDate(String.valueOf(map.get("contractEndDate") == null ? "" : map.get("contractEndDate")));
            orderDetailVo.setContractNo(String.valueOf(map.get("contractNo") == null ? "" : map.get("contractNo")));
            orderDetailVo.setContractStr(String.valueOf(map.get("contractStr") == null ? "" : map.get("contractStr")));
            orderDetailVo.setCustomerName(String.valueOf(map.get("customerName") == null ? "" : map.get("customerName")));
            orderDetailVo.setDeliveryNum(String.valueOf(map.get("deliveryNum") == null ? "" : map.get("deliveryNum")));
            orderDetailVo.setDeliveryStr(String.valueOf(map.get("deliveryStr") == null ? "" : map.get("deliveryStr")));
            orderDetailVo.setEndStr(String.valueOf(map.get("endStr") == null ? "" : map.get("endStr")));
            orderDetailVo.setFlowType(String.valueOf(map.get("flowType") == null ? "" : map.get("flowType")));
            orderDetailVo.setFlowUserName(String.valueOf(map.get("flowUserName") == null ? "" : map.get("flowUserName")));
            orderDetailVo.setLatestEndNodeName(String.valueOf(map.get("latestEndNodeName") == null ? "" : map.get("latestEndNodeName")));
            orderDetailVo.setMoney(String.valueOf(map.get("money") == null ? "" : map.get("money")));
            orderDetailVo.setNodeStr(String.valueOf(map.get("nodeStr") == null ? "" : map.get("nodeStr")));
            orderDetailVo.setNodeTermTime(String.valueOf(map.get("nodeTermTime") == null ? "" : map.get("nodeTermTime")));
            orderDetailVo.setNoticeNo(String.valueOf(map.get("noticeNo") == null ? "" : map.get("noticeNo")));
            orderDetailVo.setNoticeNum(String.valueOf(map.get("noticeNum") == null ? "" : map.get("noticeNum")));
            orderDetailVo.setNoticeStr(String.valueOf(map.get("noticeStr") == null ? "" : map.get("noticeStr")));
            orderDetailVo.setOrderNo(String.valueOf(map.get("orderNo") == null ? "" : map.get("orderNo")));
            orderDetailVo.setOrderStr(String.valueOf(map.get("orderStr") == null ? "" : map.get("orderStr")));
            orderDetailVo.setOverStr(String.valueOf(map.get("overStr") == null ? "" : map.get("overStr")));
            orderDetailVo.setPersonalName(String.valueOf(map.get("personalName") == null ? "" : map.get("personalName")));
            orderDetailVo.setProductAttr(String.valueOf(map.get("productAttr") == null ? "" : map.get("productAttr")));
            orderDetailVo.setProductName(String.valueOf(map.get("productName") == null ? "" : map.get("productName")));
            orderDetailVo.setProductUserName(String.valueOf(map.get("productUserName") == null ? "" : map.get("productUserName")));
            orderDetailVo.setPublicityNo(String.valueOf(map.get("publicityNo") == null ? "" : map.get("publicityNo")));
            orderDetailVo.setPublicityNum(String.valueOf(map.get("publicityNum") == null ? "" : map.get("publicityNum")));
            orderDetailVo.setPublicityStr(String.valueOf(map.get("publicityStr") == null ? "" : map.get("publicityStr")));
            orderDetailVo.setReceiveTime(String.valueOf(map.get("receiveTime") == null ? "" : map.get("receiveTime")));
            orderDetailVo.setSignTime(String.valueOf(map.get("signTime") == null ? "" : map.get("signTime")));
            orderDetailVo.setSignUserName(String.valueOf(map.get("signUserName") == null ? "" : map.get("signUserName")));
            orderDetailVo.setSignUserOrgName(String.valueOf(map.get("signUserOrgName") == null ? "" : map.get("signUserOrgName")));
            orderDetailVo.setStatus(String.valueOf(map.get("status") == null ? "" : map.get("status")));
            voList.add(orderDetailVo);
        }
        return voList;
    }
}
